package ore.example.pokemoninfo.utils;

import com.pixelmonmod.pixelmon.api.pokemon.Pokemon;
import com.pixelmonmod.pixelmon.api.pokemon.PokemonFactory;
import com.pixelmonmod.pixelmon.api.pokemon.stats.evolution.Evolution;
import com.pixelmonmod.pixelmon.api.pokemon.stats.evolution.conditions.EvoCondition;
import com.pixelmonmod.pixelmon.api.pokemon.stats.evolution.types.TradeEvolution;
import com.pixelmonmod.pixelmon.api.pokemon.species.Species;
import com.pixelmonmod.pixelmon.api.registries.PixelmonSpecies;
import com.pixelmonmod.pixelmon.api.util.helpers.SpriteItemHelper;
import net.minecraft.network.chat.MutableComponent;
import org.bukkit.craftbukkit.v1_21_R1.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.*;

public class TradeEvolutionFinder {

    public record PokemonPhoto(Species species, ItemStack photo) {}

    /**
     * 获取所有需要通信进化的宝可梦相片及其物种信息
     */
    public static List<PokemonPhoto> getAllTradeEvolutionPhotos() {
        Collection<Species> allSpecies = PixelmonSpecies.getAll();
        List<PokemonPhoto> result = new ArrayList<>();

        for (Species species : allSpecies) {
            if (species.getDefaultForm() == null) continue;
            List<Evolution> evolutions = species.getDefaultForm().getEvolutions();
            for (Evolution evolution : evolutions) {
                if (evolution instanceof TradeEvolution tradeEvo) {
                    Pokemon pokemon = PokemonFactory.create(species);
                    net.minecraft.world.item.ItemStack nmsSprite = SpriteItemHelper.getPhoto(pokemon);
                    ItemStack bukkitSprite = CraftItemStack.asBukkitCopy(nmsSprite);
                    List<String> evolutionConditionDescriptions = new ArrayList<>();

                    for (EvoCondition condition : evolution.conditions()) {
                        MutableComponent descriptionComponent = condition.getDescription();
                        if (descriptionComponent != null) {
                            // 正确的方法：使用 getString() 来获取实际的字符串内容
                            String descriptionString = descriptionComponent.getString();
                            // 现在 descriptionString 可能就是 "需要与朋友交换" 或 "需要达到等级 36" 这样的文本
                            evolutionConditionDescriptions.add(descriptionString);
                        }
                    }
                    ItemMeta meta = bukkitSprite.getItemMeta();
                    if (meta != null) {
                        meta.setDisplayName("§e" + species.getTranslatedName().getString());
                        List<String> lore = new ArrayList<>();
                        lore.add("§7图鉴编号: §f" + species.getDex());
                        // 添加进化条件描述到Lore
                        if (!evolutionConditionDescriptions.isEmpty()) {
                            lore.add(""); // 添加一个空行作为分隔
                            lore.add("§6§l进化条件:");
                            for (String conditionDesc : evolutionConditionDescriptions) {
                                // 确保每一行描述文本是可读的
                                lore.add("§7- " + conditionDesc);
                            }
                        }
                        lore.add("§a点击以尝试进化");
                        meta.setLore(lore);
                        bukkitSprite.setItemMeta(meta);
                    }

                    result.add(new PokemonPhoto(species, bukkitSprite));
                    break;
                }
            }
        }

        return result;
    }
}
